package AccionBoton;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;

import paginas.PaginaAspectoBoton;
import util.ImageFileView;
import util.ImageFilter;
import util.ImagePreview;

public class AccionSeleccionImagenBoton implements ActionListener{

	
	JFileChooser fc;
	private PaginaAspectoBoton _frame;
	
	
	public AccionSeleccionImagenBoton(PaginaAspectoBoton frame){
		
		_frame = frame;
		
	}
	
    public void actionPerformed(ActionEvent e) {
        //Set up the file chooser.
        if (fc == null) {
            fc = new JFileChooser(new File("lib//imagenes"));
            

	    //Add a custom file filter and disable the default
	    //(Accept All) file filter.
            fc.addChoosableFileFilter(new ImageFilter());
            fc.setAcceptAllFileFilterUsed(false);

	    //Add custom icons for file types.
            fc.setFileView(new ImageFileView());

	    //Add the preview pane.
            fc.setAccessory(new ImagePreview(fc));
        }

        //Show it.
        int returnVal = fc.showDialog(_frame,"Abrir");

        //Process the results.
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            ImageIcon imagen = new ImageIcon(file.getAbsolutePath());
            
			int alto = imagen.getIconHeight();
			double escala = (double)alto/50;
			int ancho = (int)(imagen.getIconWidth()/escala);
			Image redimensionada = imagen.getImage().getScaledInstance(ancho,
					50, Image.SCALE_DEFAULT);
			_frame.get_botonTO().setNombreIconoFichero(file.getAbsolutePath());
            _frame.get_boton().getLabelImagen().setIcon(new ImageIcon(redimensionada));
            _frame.pack();
            _frame.repaint();


        } else {
          
        }
       

        //Reset the file chooser for the next time it's shown.
        fc.setSelectedFile(null);
    }


	
	
}
